/**
 * @fileOverview hover node
 * @author huangtonger@aliyun.com
 */
const Page = require('../page');

Page.registerBehaviour('hoverNodeActived', page => {
  const graph = page.getGraph();
  let hoverItem;
  graph.behaviourOn('node:mouseenter', ev => {
    const { item } = ev;
    const shapeObj = item.getShapeObj();
    if (shapeObj.panAble !== false) {
      page.css({
        cursor: 'move'
      });
    }
    if (page.getSignal('panningItem') || page.getSignal('dragEdge') || (ev.item && ev.item.isSelected)) {
      return;
    }
    hoverItem = ev.item;
    page.setActived(hoverItem, true);
  });
  graph.behaviourOn('node:mouseleave', ev => {
    const toShape = ev.toShape;
    if (!hoverItem) {
      return;
    }
    if (toShape && toShape.isAnchor && toShape.getItem() === hoverItem || page.getSignal('dragEdge')) {
      return;
    }
    if (!hoverItem.isSelected) {
      page.setActived(hoverItem, false);
    }
    resetStatus();
  });
  function resetStatus() {
    hoverItem = undefined;
  }
});
